package tmp.antlr;

import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import org.antlr.v4.runtime.tree.TerminalNode;

import a5.HelloBaseListener;
import a5.HelloLexer;
import a5.HelloParser;
import a5.HelloParser.RContext;

public class Test5 {

	public static void main(String[] args) throws Exception{
		run("hello wang");

	}

	public static void run(String expr)throws Exception{
		ANTLRInputStream in = new ANTLRInputStream(expr);
		
		HelloLexer lexer = new HelloLexer(in);
		
		CommonTokenStream tokens = new CommonTokenStream(lexer);
		
		HelloParser exprParser = new HelloParser(tokens);
		RContext rc = exprParser.r();
		TerminalNode id = rc.ID();
		
		System.out.println(id.getText());
		
		ParseTreeWalker walker = new ParseTreeWalker();
		MyListener extractor = new MyListener(walker);
		walker.walk(extractor, rc);
	}

}

class MyListener extends HelloBaseListener{
	ParseTreeWalker walker;

	public MyListener(ParseTreeWalker walker) {
		super();
		this.walker = walker;
	}
	
	@Override
	public void visitTerminal(TerminalNode node) { 
		System.out.println("[MyListener]" + node.getText());
	}
	
}
